-- SetTeamHandler.lua
-- Modul Sider PES 2021: Set Team Name Texture Handler
-- Versi: 7.2
--
-- FIX v7.2: Ganti set_teams -> after_set_conditions.
-- ctx.stadium_server masih nil saat set_teams dipanggil.
-- after_set_conditions dipanggil setelah stadium dipilih,
-- ctx.home_team, ctx.away_team, dan ctx.stadium_server sudah tersedia.

local CONTENT_FOLDER = "content\\Team Name\\"
local STADIUM_SERVER  = "content\\stadium-server\\"

local team_names = {
    -- NATIONAL TEAMS (UEFA)
    [1]    = "ireland",
    [2]    = "northern ireland",
    [3]    = "scotland",
    [4]    = "wales",
    [5]    = "england",
    [6]    = "portugal",
    [7]    = "spain",
    [8]    = "france",
    [9]    = "belgium",
    [10]   = "netherlands",
    [11]   = "switzerland",
    [12]   = "italy",
    [13]   = "czech republic",
    [14]   = "germany",
    [15]   = "denmark",
    [16]   = "norway",
    [17]   = "sweden",
    [18]   = "finland",
    [19]   = "poland",
    [20]   = "slovakia",
    [21]   = "austria",
    [22]   = "hungary",
    [23]   = "slovenia",
    [24]   = "croatia",
    [26]   = "romania",
    [27]   = "bulgaria",
    [28]   = "greece",
    [29]   = "türkiye",
    [30]   = "ukraine",
    [31]   = "russia",
    -- NATIONAL TEAMS (AFRICA)
    [32]   = "morocco",
    [33]   = "tunisia",
    [34]   = "egypt",
    [35]   = "nigeria",
    [36]   = "cameroon",
    [37]   = "south africa",
    [38]   = "senegal",
    -- NATIONAL TEAMS (AMERICAS)
    [39]   = "united states",
    [40]   = "mexico",
    [41]   = "jamaica",
    [42]   = "costa rica",
    [43]   = "honduras",
    [44]   = "colombia",
    [45]   = "brazil",
    [46]   = "peru",
    [47]   = "chile",
    [48]   = "paraguay",
    [49]   = "uruguay",
    [50]   = "argentina",
    [51]   = "ecuador",
    -- NATIONAL TEAMS (ASIA/PACIFIC)
    [52]   = "japan",
    [53]   = "korea republic",
    [54]   = "china",
    [55]   = "iran",
    [56]   = "saudi arabia",
    [57]   = "australia",
    [59]   = "latvia",
    -- PREMIER LEAGUE
    [100]  = "manchester united",
    [101]  = "arsenal fc",
    [102]  = "chelsea fc",
    [103]  = "liverpool fc",
    [104]  = "leeds united",
    [105]  = "west ham united",
    [106]  = "newcastle united",
    [107]  = "aston villa",
    -- LA LIGA
    [108]  = "fc barcelona",
    [109]  = "real madrid f.c.",
    [110]  = "valencia cf",
    [111]  = "deportivo la coruña",
    -- LIGUE 1
    [112]  = "as monaco",
    [113]  = "olympique de marseille",
    [114]  = "paris saint-germain",
    -- EREDIVISIE
    [116]  = "ajax amsterdam",
    [117]  = "feyenoord rotterdam",
    [118]  = "psv eindhoven",
    -- SERIE A
    [119]  = "inter milan",
    [120]  = "juventus fc",
    [121]  = "ac milan",
    [122]  = "s.s. lazio",
    [123]  = "parma calcio 1913",
    [124]  = "acf fiorentina",
    [125]  = "as roma",
    -- BUNDESLIGA
    [126]  = "borussia dortmund",
    [127]  = "fc bayern münchen",
    [128]  = "bayer 04 leverkusen",
    [129]  = "hamburger sv",
    -- SÜPER LIG
    [130]  = "galatasaray sk",
    -- SCOTTISH PREMIERSHIP
    [131]  = "celtic fc",
    [132]  = "rangers fc",
    -- SUPER LEAGUE GREECE
    [133]  = "olympiakos piraeus",
    -- RUSSIA
    [135]  = "spartak moscow",
    -- BRAZIL
    [136]  = "cr vasco da gama",
    [137]  = "se palmeiras",
    -- ARGENTINA
    [138]  = "ca river plate",
    [139]  = "ca boca juniors",
    -- DEFAULT
    [140]  = "ml default europe",
    -- J.LEAGUE
    [146]  = "kashima antlers",
    [147]  = "urawa red diamonds",
    [148]  = "jef united chiba",
    [149]  = "kashiwa reysol",
    [150]  = "fc tokyo",
    [151]  = "tokyo verdy",
    [152]  = "yokohama f. marinos",
    [153]  = "shimizu s-pulse",
    [155]  = "nagoya grampus",
    [156]  = "kyoto sanga",
    [157]  = "gamba osaka",
    [158]  = "vissel kobe",
    [159]  = "sanfrecce hiroshima",
    [161]  = "mito hollyhock",
    [163]  = "kawasaki frontale",
    [168]  = "cerezo osaka",
    [169]  = "avispa fukuoka",
    -- LA LIGA
    [172]  = "atlético madrid",
    -- PREMIER LEAGUE
    [173]  = "manchester city",
    -- JUPILER PRO LEAGUE
    [174]  = "rsc anderlecht",
    -- PREMIER LEAGUE
    [176]  = "blackburn rovers",
    [177]  = "everton fc",
    [178]  = "fulham fc",
    [179]  = "tottenham hotspur",
    -- LIGUE 1
    [180]  = "aj auxerre",
    [181]  = "olympique lyonnais",
    [182]  = "rc lens",
    -- BUNDESLIGA
    [185]  = "sv werder bremen",
    -- SERIE A
    [186]  = "bologna fc 1909",
    [190]  = "udinese calcio",
    -- LIGA PORTUGAL
    [191]  = "sl benfica",
    [192]  = "fc porto",
    [193]  = "sporting cp",
    -- LA LIGA
    [194]  = "real betis balompié",
    [195]  = "celta de vigo",
    [196]  = "real sociedad",
    -- SÜPER LIG
    [197]  = "fenerbahçe sk",
    -- SUPER LEAGUE GREECE
    [198]  = "panathinaikos fc",
    -- CUSTOM
    [199]  = "custom team (eu)",
    [200]  = "custom team (asia)",
    -- CHAMPIONSHIP
    [201]  = "birmingham city fc",
    [203]  = "charlton athletic",
    [204]  = "leicester city",
    [205]  = "middlesbrough fc",
    [207]  = "southampton fc",
    [208]  = "wolverhampton wanderers",
    -- LIGUE 1/2
    [210]  = "sc bastia",
    [211]  = "ea guingamp",
    [212]  = "le mans fc",
    [213]  = "losc lille",
    [215]  = "montpellier hsc",
    [216]  = "fc nantes",
    [217]  = "ogc nice",
    [218]  = "stade rennais fc",
    [221]  = "toulouse fc",
    -- BUNDESLIGA
    [225]  = "borussia m'gladbach",
    [226]  = "eintracht frankfurt",
    [227]  = "sc freiburg",
    [231]  = "vfb stuttgart",
    [232]  = "vfl wolfsburg",
    -- SERIE A
    [234]  = "atalanta bc",
    [235]  = "empoli fc",
    [237]  = "modena fc",
    [238]  = "palermo fc",
    [240]  = "uc sampdoria",
    -- EREDIVISIE
    [242]  = "az alkmaar",
    [244]  = "fc groningen",
    [245]  = "sc heerenveen",
    [246]  = "nac breda",
    [247]  = "nec nijmegen",
    [250]  = "fc twente",
    [251]  = "fc utrecht",
    [253]  = "fc volendam",
    [256]  = "pec zwolle",
    -- LA LIGA
    [258]  = "athletic bilbao",
    [259]  = "rcd espanyol",
    [260]  = "málaga cf",
    [261]  = "rcd mallorca",
    [263]  = "ca osasuna",
    [264]  = "racing santander",
    [265]  = "sevilla fc",
    [266]  = "real valladolid",
    [267]  = "villarreal cf",
    [268]  = "real zaragoza",
    -- JUPILER PRO LEAGUE
    [269]  = "club brugge kv",
    -- RUSSIA
    [271]  = "lokomotiv moscow",
    -- SÜPER LIG
    [273]  = "beşiktaş jk",
    -- BRAZIL
    [274]  = "cruzeiro ec",
    -- K LEAGUE
    [279]  = "jeonbuk hyundai motors",
    [281]  = "pohang steelers",
    [282]  = "fc seoul",
    -- CHINA
    [295]  = "beijing fc",
    -- SERIE A/B
    [319]  = "ssc bari",
    [320]  = "cagliari calcio",
    [323]  = "genoa cfc",
    [327]  = "ssc napoli",
    [328]  = "delfino pescara",
    [333]  = "torino fc",
    [336]  = "hellas verona fc",
    -- EREDIVISIE
    [344]  = "excelsior rotterdam",
    [345]  = "fortuna sittard",
    [346]  = "go ahead eagles",
    [349]  = "heracles almelo",
    [351]  = "sparta rotterdam",
    [352]  = "sc telstar",
    -- LA LIGA 2
    [357]  = "ud almería",
    [359]  = "córdoba cf",
    [361]  = "elche cf",
    [362]  = "getafe cf",
    [363]  = "sporting gijón",
    [364]  = "ud las palmas",
    [366]  = "levante ud",
    [370]  = "rayo vallecano",
    -- PREMIER LEAGUE / CHAMPIONSHIP
    [377]  = "brighton & hove albion",
    [378]  = "burnley fc",
    [382]  = "crystal palace",
    [383]  = "derby county",
    [386]  = "ipswich town",
    [387]  = "millwall fc",
    [388]  = "norwich city",
    [389]  = "nottingham forest",
    [394]  = "sheffield wednesday",
    [395]  = "stoke city",
    [396]  = "sunderland afc",
    [398]  = "watford fc",
    [399]  = "west bromwich albion",
    -- LIGUE 1/2
    [403]  = "angers sco",
    [405]  = "sm caen",
    [407]  = "clermont foot 63",
    [412]  = "stade lavallois",
    [413]  = "le havre ac",
    [414]  = "fc lorient",
    [415]  = "as nancy-lorraine",
    [418]  = "as saint-étienne",
    [420]  = "estac troyes",
    -- BUNDESLIGA
    [436]  = "fsv mainz 05",
    -- AFRICA
    [551]  = "al ahly sc",
    -- KAZAKHSTAN
    [554]  = "fc kairat almaty",
    -- ALL STARS
    [900]  = "a.c. milan all stars",
    [901]  = "f.c. inter all stars",
    [902]  = "juventus f.c. all stars",
    [903]  = "real madrid c.f. all stars",
    [904]  = "f.c. barcelona all stars",
    [905]  = "manchester united f.c. all stars",
    [906]  = "chelsea f.c. all stars",
    [907]  = "arsenal f.c. all stars",
    [908]  = "liverpool f.c. all stars",
    [909]  = "fc bayern münchen all stars",
    -- NATIONAL TEAMS (ASIA)
    [1001] = "afghanistan",
    [1002] = "bahrain",
    [1003] = "bangladesh",
    [1006] = "cambodia",
    [1007] = "hong kong",
    [1008] = "india",
    [1009] = "indonesia",
    [1010] = "iraq",
    [1011] = "jordan",
    [1012] = "north korea",
    [1013] = "kuwait",
    [1015] = "lebanon",
    [1017] = "malaysia",
    [1020] = "myanmar",
    [1021] = "nepal",
    [1022] = "oman",
    [1023] = "pakistan",
    [1025] = "philippines",
    [1026] = "qatar",
    [1027] = "singapore",
    [1029] = "syria",
    [1031] = "thailand",
    [1032] = "united arab emirates",
    [1033] = "vietnam",
    [1034] = "yemen",
    [1036] = "tajikistan",
    -- NATIONAL TEAMS (AFRICA)
    [1040] = "algeria",
    [1041] = "angola",
    [1042] = "benin",
    [1044] = "burkina faso",
    [1046] = "cabo verde",
    [1050] = "congo dr",
    [1051] = "côte d'ivoire",
    [1056] = "gabon",
    [1058] = "ghana",
    [1059] = "guinea",
    [1061] = "kenya",
    [1065] = "madagascar",
    [1067] = "mali",
    [1080] = "tanzania",
    [1081] = "togo",
    [1082] = "uganda",
    [1083] = "zambia",
    [1084] = "zimbabwe",
    [1087] = "congo",
    -- NATIONAL TEAMS (CONCACAF)
    [1099] = "canada",
    [1104] = "el salvador",
    [1107] = "guatemala",
    [1108] = "haiti",
    [1113] = "panama",
    [1118] = "trinidad and tobago",
    [1124] = "curacao",
    -- NATIONAL TEAMS (SOUTH AMERICA)
    [1128] = "bolivia",
    [1129] = "venezuela",
    -- NATIONAL TEAMS (OCEANIA)
    [1141] = "new zealand",
    -- NATIONAL TEAMS (UEFA SMALL)
    [1164] = "israel",
    [1165] = "albania",
    [1166] = "andorra",
    [1167] = "armenia",
    [1168] = "azerbaijan",
    [1169] = "belarus",
    [1170] = "bosnia and herzegovina",
    [1171] = "cyprus",
    [1172] = "estonia",
    [1173] = "faroe islands",
    [1174] = "georgia",
    [1175] = "iceland",
    [1176] = "kazakhstan",
    [1177] = "liechtenstein",
    [1178] = "lithuania",
    [1179] = "luxembourg",
    [1180] = "north macedonia",
    [1181] = "malta",
    [1182] = "moldova",
    [1183] = "san marino",
    [1184] = "uzbekistan",
    -- JUPILER PRO LEAGUE
    [1195] = "krc genk",
    [1196] = "kaa gent",
    [1197] = "standard liège",
    [1200] = "kv mechelen",
    -- CROATIA
    [1203] = "gnk dinamo zagreb",
    -- DENMARK
    [1207] = "fc københavn",
    [1208] = "fc nordsjælland",
    -- GREECE
    [1212] = "paok thessaloniki",
    -- ROMANIA
    [1216] = "fcsb",
    -- RUSSIA
    [1217] = "cska moskva",
    [1218] = "zenit st. petersburg",
    -- SCOTLAND
    [1219] = "aberdeen fc",
    [1220] = "dundee united",
    [1221] = "heart of midlothian",
    [1222] = "hibernian fc",
    -- SERBIA
    [1223] = "red star belgrade",
    -- SWITZERLAND
    [1228] = "grasshopper club zürich",
    -- TURKEY
    [1230] = "genclerbirligi ankara",
    -- ARGENTINA
    [1236] = "argentinos juniors",
    [1237] = "racing club",
    [1238] = "estudiantes de la plata",
    [1239] = "gimnasia la plata",
    [1240] = "ca independiente",
    [1241] = "newell's old boys",
    [1242] = "ca rosario central",
    [1243] = "ca san lorenzo",
    [1244] = "ca vélez sarsfield",
    -- BRAZIL
    [1245] = "atlético mineiro",
    [1246] = "botafogo fr",
    [1247] = "sc corinthians",
    [1248] = "cr flamengo",
    [1249] = "fluminense fc",
    [1250] = "grêmio fbpa",
    [1252] = "sc internacional",
    [1254] = "santos fc",
    [1255] = "são paulo fc",
    -- CHILE
    [1256] = "csd colo-colo",
    -- COLOMBIA
    [1257] = "américa de cali",
    [1258] = "millonarios fc",
    -- MEXICO
    [1264] = "cf américa",
    -- CHAMPIONSHIP
    [1327] = "queens park rangers",
    -- LIGUE 1/2
    [1328] = "dijon fco",
    [1329] = "stade brestois",
    [1330] = "stade de reims",
    -- IRAN
    [1341] = "sepahan sc",
    -- SAUDI ARABIA
    [1348] = "al shabab club",
    [1349] = "al ahli saudi fc",
    [1350] = "al ittihad club",
    -- SERIE B
    [1362] = "cesena fc",
    -- INDONESIA
    [1461] = "indonesia classics",
    -- QATAR
    [1488] = "al gharafa sc",
    -- SAUDI ARABIA
    [1489] = "al hilal sfc",
    -- THAILAND
    [1493] = "buriram united",
    -- NATIONAL TEAMS
    [1550] = "serbia",
    [1551] = "montenegro",
    -- IRAN
    [1576] = "esteghlal fc",
    -- AUSTRIA
    [1586] = "red bull salzburg",
    -- CHAMPIONSHIP
    [1589] = "hull city",
    -- SERIE B
    [1600] = "spezia calcio",
    -- MEXICO
    [1700] = "deportivo guadalajara",
    -- SWEDEN
    [1702] = "malmö ff",
    -- SWITZERLAND
    [1706] = "fc basel 1893",
    -- RUSSIA
    [1753] = "dinamo moskva",
    -- CHAMPIONSHIP
    [1760] = "bristol city",
    -- LA LIGA 2
    [1765] = "granada cf",
    -- MEXICO
    [1775] = "pumas unam",
    [1782] = "tigres uanl",
    -- LIGA PORTUGAL
    [1804] = "vitória guimarães",
    -- DENMARK
    [1832] = "brøndby if",
    -- J.LEAGUE
    [1881] = "fagiano okayama",
    -- SAUDI ARABIA
    [1902] = "al ettifaq fc",
    -- CHAMPIONSHIP
    [1909] = "swansea city",
    -- SERIE A
    [1919] = "us sassuolo",
    -- ARGENTINA
    [1922] = "ca huracán",
    [1924] = "godoy cruz",
    [1926] = "club atlético tigre",
    [1927] = "ca banfield",
    [1929] = "club atlético lanús",
    -- BRAZIL
    [1930] = "athletico paranaense",
    [1931] = "coritiba fbc",
    [1937] = "ec vitória",
    -- RUSSIA
    [1941] = "rubin kazan",
    -- LIGA PORTUGAL
    [1944] = "cd nacional",
    -- SÜPER LIG
    [1945] = "trabzonspor",
    -- SWITZERLAND
    [1950] = "bsc young boys",
    [1955] = "fc sion",
    [1957] = "fc zürich",
    [1958] = "servette fc",
    -- LIGA PORTUGAL
    [1974] = "sporting braga",
    [1979] = "rio ave fc",
    -- SCOTLAND
    [1985] = "kilmarnock fc",
    [1986] = "motherwell fc",
    [1987] = "st. mirren fc",
    -- SÜPER LIG
    [1989] = "antalyaspor",
    [1995] = "başakşehir fk",
    [1996] = "kayserispor",
    -- JUPILER PRO LEAGUE
    [2009] = "cercle brugge",
    [2010] = "rsc charleroi",
    [2019] = "zulte waregem",
    -- CZECH REPUBLIC
    [2037] = "fc viktoria plzeň",
    -- CHINA
    [2049] = "chengdu rongcheng",
    -- DENMARK
    [2067] = "aarhus gf",
    [2069] = "fc midtjylland",
    [2070] = "odense boldklub",
    [2071] = "randers fc",
    -- AUSTRIA
    [2081] = "sk sturm graz",
    -- LA LIGA
    [2187] = "girona fc",
    [2188] = "sd huesca",
    -- CHILE
    [2191] = "universidad católica",
    [2192] = "audax italiano",
    -- COLOMBIA
    [2193] = "atlético nacional",
    [2209] = "universidad de chile",
    [2210] = "independiente medellín",
    -- RUSSIA
    [2229] = "fk rostov",
    -- COLOMBIA
    [2285] = "junior fc",
    -- SERIE B
    [2320] = "calcio padova",
    -- COLOMBIA
    [2361] = "deportes tolima",
    -- J.LEAGUE
    [2372] = "machida zelvia",
    [2373] = "v-varen nagasaki",
    -- HUNGARY
    [2375] = "ferencvárosi tc",
    -- LIGA PORTUGAL
    [2380] = "fc arouca",
    [2383] = "gd estoril praia",
    [2387] = "gil vicente fc",
    [2388] = "moreirense fc",
    [2391] = "cd santa clara",
    -- BRAZIL
    [2450] = "américa fc mg",
    [2453] = "ec bahia",
    [2454] = "ceará sc",
    [2459] = "red bull bragantino",
    -- DEFAULT
    [2469] = "ml default latinamerica",
    -- SERIE B
    [2517] = "ss juve stabia",
    -- ARGENTINA
    [2536] = "club atlético belgrano",
    [2538] = "club atlético unión",
    -- CHILE
    [2541] = "o'higgins fc",
    -- QATAR
    [2567] = "al duhail sc",
    -- SAUDI ARABIA
    [2576] = "al fateh sc",
    [2577] = "al nassr fc",
    [2580] = "al taawoun fc",
    -- IRAN
    [2602] = "tractor fc",
    -- LIGA PORTUGAL
    [2614] = "cd tondela",
    -- SPAIN
    [2616] = "cd mirandés",
    -- RUSSIA
    [2618] = "fk krasnodar",
    -- SCOTLAND
    [2621] = "dundee fc",
    -- SÜPER LIG
    [2625] = "kasimpaşa sk",
    -- THAILAND
    [2643] = "muangthong united",
    -- COLOMBIA
    [2657] = "independiente santa fe",
    -- DEFAULT
    [2696] = "ml default asia",
    -- CLASSICS
    [2701] = "legend classic",
    [2702] = "we classics",
    [2703] = "european classics",
    [2704] = "world classics",
    -- CHILE
    [2707] = "coquimbo unido",
    -- ARGENTINA
    [2717] = "ca aldosivi",
    [2719] = "atlético tucumán",
    [2722] = "defensa y justicia",
    [2726] = "independiente rivadavia",
    [2727] = "instituto ac córdoba",
    [2730] = "ca sarmiento",
    -- BELGIUM
    [3095] = "raal la louvière",
    [3099] = "fcv dender",
    -- PREMIER LEAGUE
    [4071] = "afc bournemouth",
    -- CHINA
    [4094] = "shanghai port",
    -- BRAZIL
    [4108] = "a chapecoense de futebol",
    -- SINGAPORE
    [4115] = "bg tampines rovers",
    -- LIGUE 1/2
    [4123] = "fc metz",
    -- BUNDESLIGA
    [4124] = "fc augsburg",
    [4126] = "tsg 1899 hoffenheim",
    [4137] = "fc köln",
    [4139] = "fc st. pauli",
    [4140] = "fc union berlin",
    -- SERIE B
    [4141] = "us avellino",
    -- RUSSIA
    [4143] = "krylya sovetov samara",
    -- LA LIGA
    [4145] = "deportivo alavés",
    [4146] = "sd eibar",
    -- MLS
    [4148] = "chicago fire fc",
    [4150] = "colorado rapids",
    [4151] = "columbus crew",
    [4152] = "d.c. united",
    [4153] = "fc dallas",
    [4154] = "houston dynamo fc",
    [4155] = "los angeles galaxy",
    [4156] = "cf montréal",
    [4157] = "new england revolution",
    [4158] = "new york red bulls",
    [4159] = "philadelphia union",
    [4160] = "portland timbers",
    [4161] = "real salt lake",
    [4162] = "san jose earthquakes",
    [4163] = "seattle sounders",
    [4164] = "sporting kansas city",
    [4165] = "toronto fc",
    [4166] = "vancouver whitecaps",
    -- A-LEAGUE
    [4167] = "melbourne city",
    -- IRAQ
    [4170] = "al shorta sc",
    -- QATAR
    [4173] = "al sadd sc",
    -- K LEAGUE
    [4174] = "ulsan hd",
    -- THAILAND
    [4175] = "chonburi fc",
    -- UAE
    [4176] = "shabab al ahli club",
    -- UZBEKISTAN
    [4178] = "nasaf qarshi",
    -- PREMIER LEAGUE
    [4180] = "brentford fc",
    -- CHAMPIONSHIP
    [4183] = "coventry city",
    [4192] = "preston north end",
    [4194] = "sheffield united",
    -- LIGUE 2
    [4200] = "amiens sc",
    [4201] = "us boulogne",
    [4206] = "usl dunkerque",
    [4211] = "paris fc",
    [4212] = "red star fc",
    [4213] = "rc strasbourg alsace",
    -- SERIE B
    [4218] = "carrarese calcio",
    [4219] = "como 1907",
    [4220] = "us cremonese",
    [4225] = "ac reggiana 1919",
    [4228] = "fc südtirol",
    [4229] = "venezia fc",
    [4230] = "virtus entella",
    [4233] = "us catanzaro",
    [4234] = "frosinone calcio",
    [4237] = "us lecce",
    [4241] = "pisa sporting club",
    -- SEGUNDA DIVISION
    [4247] = "burgos cf",
    [4252] = "cultural leonesa",
    [4260] = "real oviedo",
    [4272] = "cd leganés",
    [4276] = "sociedad b",
    [4302] = "albacete balompié",
    [4308] = "cádiz cf",
    -- AZERBAIJAN
    [4326] = "qarabağ fk",
    -- BULGARIA
    [4355] = "ludogorets razgrad",
    -- LEAGUE ONE
    [4365] = "portsmouth fc",
    -- LIGUE 2
    [4370] = "grenoble foot 38",
    [4372] = "rodez af",
    [4373] = "fc rouen 1899",
    -- SERIE B
    [4379] = "mantova 1911",
    -- SEGUNDA DIVISION
    [4395] = "cd castellón",
    -- SERIE A
    [4914] = "ac monza",
    -- SWITZERLAND
    [4937] = "fc st. gallen 1879",
    [4938] = "fc thun 1898",
    -- THAILAND
    [4941] = "bg pathum united",
    -- MALAYSIA
    [4948] = "johor darul ta'zim",
    -- UAE
    [4953] = "al wahda fc",
    -- JORDAN
    [4954] = "al wehdat sc",
    -- SWITZERLAND
    [4962] = "fc luzern",
    [4964] = "fc lausanne-sport",
    [4965] = "fc lugano",
    [4968] = "fc winterthur",
    [4971] = "the old boys",
    -- ARGENTINA
    [4995] = "central córdoba sde",
    -- BUNDESLIGA
    [5009] = "fc heidenheim 1846",
    [5010] = "rb leipzig",
    -- LIGA PORTUGAL
    [5028] = "fc famalicão",
    -- ARGENTINA
    [5046] = "club atlético talleres",
    -- MLS
    [5061] = "new york city",
    [5062] = "orlando city",
    -- CHAMPIONSHIP
    [5086] = "oxford united",
    -- LIGUE 2
    [5099] = "pau fc",
    -- SELECTION
    [5129] = "world selection",
    -- HONG KONG
    [5133] = "eastern sc",
    -- K LEAGUE
    [5135] = "jeju sk",
    -- BRAZIL
    [5137] = "ec juventude",
    [5143] = "fortaleza ec",
    [5145] = "clube do remo",
    -- CHINA
    [5173] = "shanghai shenhua",
    -- CZECH REPUBLIC
    [5189] = "slavia prague",
    -- JUPILER PRO LEAGUE
    [5191] = "royal antwerp fc",
    [5194] = "sint-truiden vv",
    -- RUSSIA
    [5196] = "akhmat grozny",
    -- SÜPER LIG
    [5202] = "alanyaspor",
    [5203] = "göztepe sk",
    [5204] = "konyaspor",
    -- JUPILER PRO LEAGUE
    [5217] = "o.h leuven",
    [5220] = "union saint gilloise",
    [5221] = "kvc westerlo",
    -- DENMARK
    [5225] = "silkeborg if",
    [5226] = "sønderjyske fodbold",
    [5229] = "fc fredericia",
    [5235] = "vejle boldklub",
    [5237] = "viborg ff",
    -- NORWAY
    [5253] = "fk bodø/glimt",
    -- RUSSIA
    [5295] = "baltika kaliningrad",
    [5296] = "fc sochi",
    [5300] = "fc pari nn",
    [5301] = "fk orenburg",
    -- SCOTLAND
    [5317] = "falkirk fc",
    [5319] = "livingston fc",
    -- SÜPER LIG
    [5354] = "çaykur rizespor",
    [5356] = "gaziantep fk",
    [5361] = "samsunspor",
    -- ARGENTINA
    [5366] = "club deportivo riestra",
    -- UAE
    [5388] = "al wasl fc",
    -- THAILAND
    [5394] = "true bangkok united",
    [5395] = "singha chiangrai united",
    [5397] = "nakhonratchasima fc",
    [5401] = "port fc",
    [5402] = "pt prachuap fc",
    [5403] = "ratchaburi fc",
    [5404] = "sukhothai fc",
    [5416] = "rayong fc",
    -- NATIONAL TEAMS (SMALL)
    [5439] = "gibraltar",
    [5440] = "kosovo",
    -- ARGENTINA
    [5454] = "ca platense",
    -- IRAQ
    [5591] = "al zawraa sc",
    -- SINGAPORE
    [5598] = "lion city sailors",
    -- TAJIKISTAN
    [5599] = "istiqlol dushanbe",
    -- THAILAND
    [5600] = "ayutthaya united",
    -- LIGA PORTUGAL
    [5633] = "casa pia ac",
    -- ANDORRA
    [5644] = "fc andorra",
    -- SÜPER LIG
    [5652] = "fatih karagümrük",
    -- ARGENTINA
    [5657] = "ca barracas central",
    -- THAILAND
    [5675] = "uthai thani fc",
    -- UAE
    [5676] = "sharjah fc",
    -- LIGUE 2
    [5685] = "fc annecy",
    -- THAILAND
    [5700] = "kanchanaburi power fc",
    -- MLS
    [5736] = "atlanta united fc",
    [5737] = "fc cincinnati",
    [5738] = "inter miami cf",
    [5739] = "nashville sc",
    [5740] = "austin fc",
    [5741] = "los angeles fc",
    [5742] = "minnesota united fc",
    -- NATIONAL TEAMS
    [5751] = "kyrgyzstan",
    [5757] = "palestine",
    -- BRAZIL
    [5767] = "mirassol fc",
    -- INDIA
    [5769] = "fc goa",
    -- PHILIPPINES
    [5775] = "kaya fc-iloilo",
    -- SÜPER LIG
    [5820] = "eyüpspor",
    [5821] = "kocaelispor",
    -- THAILAND
    [5829] = "lamphun warriors fc",
    -- MLS
    [5832] = "charlotte fc",
    -- LIGA PORTUGAL
    [5844] = "estrela da amadora",
    [5845] = "fc alverca",
    -- TURKMENISTAN
    [5875] = "ahal fc",
    -- LIGUE 2
    [5886] = "fc versailles 78",
    -- CEUTA
    [5895] = "ad ceuta fc",
    -- MLS
    [5903] = "st. louis city sc",
    -- K LEAGUE
    [5912] = "daejeon hana citizen",
    [5913] = "gangwon fc",
    [5914] = "gwangju fc",
    [5915] = "incheon united",
    [5918] = "bucheon fc 1995",
    [5923] = "fc anyang",
    [5924] = "gimcheon sangmu",
    -- LIGA PORTUGAL
    [5954] = "avs futebol sad",
    -- NATIONAL TEAMS
    [5987] = "somalia",
    [5988] = "laos",
    -- BAHRAIN
    [6036] = "al khaldiya sc",
    -- INDIA
    [6038] = "mohun bagan super giant",
    -- JORDAN
    [6040] = "al hussein sc",
    -- MALAYSIA
    [6041] = "selangor fc",
    -- VIETNAM
    [6044] = "thep xanh nam dinh fc",
    -- NATIONAL TEAMS
    [6051] = "brunei darussalam",
    [6052] = "timor-leste",
    [6061] = "botswana",
    -- WALES
    [6069] = "wrexham afc",
    -- MLS
    [6084] = "san diego fc",
    -- VIETNAM
    [6114] = "cong an hanoi fc",
    -- A-LEAGUE
    [6189] = "macarthur fc",
    -- BAHRAIN
    [6190] = "al muharraq sc",
    -- HONG KONG
    [6191] = "tai po fc",
    -- SAUDI ARABIA
    [6193] = "al ahli sc",
    -- TURKMENISTAN
    [6195] = "arkadag fk",
    -- UZBEKISTAN
    [6196] = "fc andijan",
    -- SAUDI ARABIA
    [9000] = "al hazem sc",
    -- CYPRUS
    [9001] = "pafos fc",
    -- SAUDI ARABIA
    [9002] = "al riyadh sc",
    [9003] = "damac fc",
    [9004] = "neom sc",
    [9005] = "al okhdood club",
    [9006] = "al najma sc",
    [9007] = "al khaleej club",
    [9010] = "al kholood club",
    [9012] = "al qadsiah fc",
    [9014] = "al fayha fc",
    -- RUSSIA
    [9015] = "dinamo makhachkala",
    [9016] = "akron tolyatti",
    -- INDONESIA
    [9045] = "psim yogyakarta",
    [9046] = "persela lamongan",
    [9047] = "fc bekasi city",
    [9050] = "psps pekanbaru",
    [9051] = "persiba balikpapan",
    [9053] = "semen padang fc",
    [9056] = "persekat tegal",
    [9058] = "deltras fc",
    [9059] = "malut united fc",
    [9061] = "psbs biak",
    [9062] = "sriwijaya fc",
    [9065] = "garudayaksa fc",
    [9066] = "persijap jepara",
    [9067] = "persipal fc",
    [9068] = "persikad depok",
    [9070] = "persiraja banda aceh",
    [9090] = "arema fc",
    [9091] = "bali united fc",
    [9092] = "ps barito putera",
    [9093] = "bhayangkara plfc",
    [9094] = "borneo fc samarinda",
    [9095] = "dewa united banten fc",
    [9096] = "madura united fc",
    [9097] = "persebaya surabaya",
    [9098] = "persib bandung",
    [9099] = "persija jakarta",
    [9100] = "persik kediri",
    [9102] = "persipura jayapura",
    [9103] = "persis solo",
    [9104] = "persita tangerang",
    [9105] = "psis semarang",
    [9106] = "psm makassar",
    [9107] = "psms medan",
    [9108] = "pss sleman",
    [9110] = "adhyaksa banten fc",
    [9112] = "persibo bojonegoro",
    [9113] = "sumsel united",
    [9114] = "persikota tangerang",
    [9115] = "persiku kudus",
    [9116] = "kendal tornado fc",
}

local sider_dir = ""

-- ffi tersedia sebagai global di Sider LuaJIT (tanpa require/pcall)
-- Digunakan untuk handle nama file beraksesn (é, ü, ó, dll)
if ffi ~= nil then
    ffi.cdef[[
        unsigned int  GetFileAttributesW(const unsigned short* fn);
        int           CopyFileW(const unsigned short* src, const unsigned short* dst, int fail_if_exists);
        int           MultiByteToWideChar(unsigned int cp, unsigned long flags,
                          const char* mb, int mb_sz, unsigned short* wc, int wc_sz);
    ]]
end

-- helper: Lua string -> mutable char* buffer (diperlukan karena Sider LuaJIT pakai char* bukan const char*)
local function str_to_charbuf(s)
    local mb = ffi.new("char[?]", #s + 1)
    ffi.copy(mb, s)
    return mb
end

-- UTF-8 string -> wide char  (untuk nama tim dari source Lua UTF-8)
local function to_wchar_u8(s)
    local n = #s * 2 + 2
    local buf = ffi.new("unsigned short[?]", n)
    local mb = str_to_charbuf(s)
    local len = ffi.C.MultiByteToWideChar(65001, 0, mb, #s, buf, n)
    if len == 0 then return nil end
    buf[len] = 0
    return buf
end

-- ANSI string -> wide char  (untuk path dari Sider runtime, misal "Bernabéu")
local function to_wchar_mb(s)
    local n = #s * 2 + 2
    local buf = ffi.new("unsigned short[?]", n)
    local mb = str_to_charbuf(s)
    local len = ffi.C.MultiByteToWideChar(0, 0, mb, #s, buf, n)
    if len == 0 then return nil end
    buf[len] = 0
    return buf
end

local function file_exists(path)
    if ffi ~= nil then
        local w = to_wchar_u8(path)
        if w and ffi.C.GetFileAttributesW(w) ~= 0xFFFFFFFF then return true end
    end
    local f = io.open(path, "rb")
    if f then f:close() return true end
    return false
end

local function copy_file(src, dst)
    -- src = path tim (UTF-8 dari Lua source)
    -- dst = path stadium (ANSI dari Sider runtime)
    if ffi ~= nil then
        local ws = to_wchar_u8(src)
        local wd = to_wchar_mb(dst)
        if ws and wd and ffi.C.CopyFileW(ws, wd, 0) ~= 0 then return true end
    end
    -- Fallback io.open: bekerja untuk nama tim ASCII + path ANSI
    local f = io.open(src, "rb")
    if not f then return false end
    local data = f:read("*all")
    f:close()
    local g = io.open(dst, "wb")
    if not g then return false end
    g:write(data)
    g:close()
    return true
end

local function place_texture(team_id, dst_path, default_path)
    local name = team_names[team_id]
    if name then
        local src = sider_dir .. CONTENT_FOLDER .. name .. ".ftex"
        if file_exists(src) then
            local ok, err = copy_file(src, dst_path)
            if ok then
                log("  -> OK: " .. name .. ".ftex")
            else
                log("  -> GAGAL copy: " .. (err or "?"))
            end
            return
        else
            log("  -> FILE TIDAK ADA: " .. src)
        end
    else
        log("  -> ID " .. (team_id or 0) .. " tidak ada di database")
    end
    if file_exists(default_path) then
        copy_file(default_path, dst_path)
        log("  -> Restore placeholder")
    end
end

-- ============================================================
local m = {}

function m.init(ctx)
    sider_dir = ctx.sider_dir or ""
    if ffi ~= nil then
        log("SetTeamHandler v7.6 init OK. [Mode: FFI Unicode]")
    else
        log("SetTeamHandler v7.6 init OK. [Mode: io.open only]")
    end
    ctx.register("after_set_conditions", m.after_set_conditions)
end

function m.after_set_conditions(ctx)
    local home_id = ctx.home_team or 0
    local away_id = ctx.away_team or 0

    log(string.format("SetTeamHandler [AFTER_SET_CONDITIONS] Home:%d Away:%d", home_id, away_id))

    local ss = ctx.stadium_server
    if not ss then
        log("  -> ctx.stadium_server nil, skip")
        return
    end

    local tex_dir = sider_dir .. STADIUM_SERVER .. ss.path ..
        "\\Asset\\model\\bg\\st" .. ss.id ..
        "\\sourceimages\\tga\\#windx11\\"
    log("  Tex dir: " .. tex_dir)

    local home_dst = tex_dir .. "Tim_Home.ftex"
    local away_dst = tex_dir .. "Tim_Away.ftex"
    local home_def = tex_dir .. "Tim_Home_default.ftex"
    local away_def = tex_dir .. "Tim_Away_default.ftex"

    -- Backup placeholder pertama kali
    if not file_exists(home_def) and file_exists(home_dst) then
        copy_file(home_dst, home_def)
        log("  Backup Tim_Home -> Tim_Home_default")
    end
    if not file_exists(away_def) and file_exists(away_dst) then
        copy_file(away_dst, away_def)
        log("  Backup Tim_Away -> Tim_Away_default")
    end

    log("  [HOME]")
    place_texture(home_id, home_dst, home_def)
    log("  [AWAY]")
    place_texture(away_id, away_dst, away_def)
end

return m
